﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using GeneratorLib.DB;
using GeneratorLib.DB.Connectios;
using GeneratorLib.DB.Schemas;
using Pasys.PresentationCore;

namespace Generator.Generators
{
    public partial class MVCEditModel : GeneratorCodeBase
    {
        //private
        //private ICommandBusService commandBus;

        //public IConnection Connection { get; set; }
        //private TableInfo _table;
        //public TableInfo Table
        //{
        //    get
        //    {
        //        return _table;
        //    }
        //    set
        //    {
        //        _table = value;
        //        if (value != null)
        //        {
        //            this.Text = string.Format("{0}", value.TableName);
        //        }
        //    }
        //}

        //public void AddToolStripItem(ToolStripItem item)
        //{

        //}

        //public string GeneratorName { get; set; }


        private bool _useCommunicationWrapper = true;
        private bool _useProperty = true;
        private bool _changeDateTimeToString = true;

        private ToolStripItem _lastActiveItem = null;
        public MVCEditModel()
        {
            InitializeComponent();
            this.GeneratorName = "MVC EditModel Generator";
            AddToolItems();
        }

        private void AddToolItems()
        {
            ToolStripMenuItem item0 = new ToolStripMenuItem("EditModel") { Tag = 0 };
            ToolStripMenuItem item1 = new ToolStripMenuItem("EditRepository") { Tag = 1 };
            ToolStripMenuItem item2 = new ToolStripMenuItem("Model And Repository") { Tag = 2 };



            //ToolStripSeparator item2 = new ToolStripSeparator();
            //item2.Tag = 2;
            //ToolStripMenuItem item3 = new ToolStripMenuItem("ListModel");
            //item3.Tag = 3;
            //ToolStripMenuItem item4 = new ToolStripMenuItem("ListRepository");
            //item4.Tag = 4;
            //ToolStripMenuItem item5 = new ToolStripMenuItem("FillInputData");
            //item5.Tag = 5;

            this.AddToolStripItem(item0);
            this.AddToolStripItem(item1);
            this.AddToolStripItem(item2);

            this.AddToolStripItem(new ToolStripSeparator());

            this.AddToolStripItem(new ToolStripMenuItem("Use CommunicationWrapper") { Tag = 1000, Checked = _useCommunicationWrapper });
            this.AddToolStripItem(new ToolStripMenuItem("Add Property") { Tag = 1001, Checked = _useProperty });
            this.AddToolStripItem(new ToolStripMenuItem("Change DateTime ToString") { Tag = 1002, Checked = _changeDateTimeToString });

            //this.AddToolStripItem(item2);
            //this.AddToolStripItem(item3);
            //this.AddToolStripItem(item4);
        }



        public override void Generate()
        {
            base.Generate();
        }

        public override void Generate(ToolStripItem item)
        {
            base.Generate(item);


            //this.Table = MYSQLUtility.GetTableInfo(this.Connection, this.Table.TableName);
            txtCode.ResetText();
            CodeBuilder.Clear();
            AppendUsing();
            AppendNameSpaceStart();
            switch (Convert.ToInt32(item.Tag))
            {
                case 0:
                    CreateEditModel();
                    _lastActiveItem = item;
                    break;
                case 1:
                    CreateEditRepository();
                    _lastActiveItem = item;
                    break;
                case 2:
                    CreateEditModel();
                    CreateEditRepository();
                    _lastActiveItem = item;
                    break;
                case 1000:
                    _useCommunicationWrapper = !_useCommunicationWrapper;
                    ((ToolStripMenuItem)item).Checked = _useCommunicationWrapper;
                    if (_lastActiveItem != null)
                    {
                        Generate(_lastActiveItem);
                    }
                    break;
                case 1001:
                    _useProperty = !_useProperty;
                    ((ToolStripMenuItem)item).Checked = _useProperty;
                    if (_lastActiveItem != null)
                    {
                        Generate(_lastActiveItem);
                    }
                    break;
                case 1002:
                    _changeDateTimeToString = !_changeDateTimeToString;
                    ((ToolStripMenuItem)item).Checked = _changeDateTimeToString;
                    if (_lastActiveItem != null)
                    {
                        Generate(_lastActiveItem);
                    }
                    break;

                default:
                    break;
            }

            AppendNameSpaceEnd();
            txtCode.Text = CodeBuilder.ToString();
            txtCode.Refresh();
        }

        private void CreateEditModel()
        {
            var res = from s in this.Connection.Relations
                      where
                          s.ForeignTable.TableName.ToLower().CompareTo(this.Table.TableName.ToLower()) == 0
                      select s;


            TableInfoAddin tableAddin = this.Connection.FindTableAddin(this.Table.TableName);

            AppandClassStart(this.Table.ModelEditName, "", string.Format("{0}Modelデータ", tableAddin.TableDisplayName));

            foreach (FieldInfo field in Table.Fields)
            {
                FieldInfoAddin addin = tableAddin.FindFieldAddin(field.FieldName);

                CodeBuilder.AppendLine("        /// <summary>");
                if (addin != null)
                    CodeBuilder.AppendLine("        /// {0}", addin.DisplayName);
                CodeBuilder.AppendLine("        /// </summary>");
                if (addin != null && _useProperty)
                    CodeBuilder.AppendLine("        [Display(Name = \"{0}\")]", addin.GetDisplay());
                if (!field.Nullabled && addin != null && _useProperty)
                    CodeBuilder.AppendLine("        [Required(ErrorMessage=\"{0}を入力してください。\")]", addin.GetDisplay());

                if (field.FieldType.MapName.ToLower().CompareTo("datetime") == 0 && _useProperty)
                    CodeBuilder.AppendLine("        [DataType(DataType.Date)]");
                CodeBuilder.AppendLine("        {0}", getSingleDeclareForSearchModel(field));
            }

            CodeBuilder.AppendLine("");

            foreach (var r in res)
            {
                TableInfo t = Connection.FindTable(r.PrimaryTable.TableName);
                TableInfoAddin tAddin = this.Connection.FindTableAddin(r.PrimaryTable.TableName);
                FieldInfoAddin addin = tAddin.FindFieldAddin(r.PrimaryTable.ValueFieldName);

                CodeBuilder.AppendLine("        /// <summary>");
                if (addin != null)
                {
                    CodeBuilder.AppendLine("        /// {0}", addin.DisplayName);
                }
                CodeBuilder.AppendLine("        /// </summary>");
                CodeBuilder.AppendLine("        public List<SelectListItem> {0} {{ get; set; }} //   {0}リスト", t.EntityListName);
            }

            CodeBuilder.AppendLine("        public bool Display");
            CodeBuilder.AppendLine("        {");
            CodeBuilder.AppendLine("            get");
            CodeBuilder.AppendLine("            {");
            CodeBuilder.AppendLine("                return DisplayFlag == DbValues.DisplayFlag.Display;");
            CodeBuilder.AppendLine("            }");
            CodeBuilder.AppendLine("            set");
            CodeBuilder.AppendLine("            {");
            CodeBuilder.AppendLine("                DisplayFlag = value ? DbValues.DisplayFlag.Display : DbValues.DisplayFlag.Hide;");
            CodeBuilder.AppendLine("            }");
            CodeBuilder.AppendLine("        }");

            CodeBuilder.AppendLine("        public string btnName { get; set; }");

            CodeBuilder.AppendLine("        public {0}()",this.Table.ModelEditName);
            CodeBuilder.AppendLine("        {");
            CodeBuilder.AppendLine("            Display = true;");
            CodeBuilder.AppendLine("        }");

            AppandClassEnd();

        }

        private void CreateEditRepository()
        {
            var res = from s in this.Connection.Relations
                      where
                          s.ForeignTable.TableName.ToLower().CompareTo(this.Table.TableName.ToLower()) == 0
                      select s;

            TableInfoAddin tableAddin = this.Connection.FindTableAddin(this.Table.TableName);
            FieldInfo keyField = this.Table.GetKeyField();

            //AppendUsing();
            //AppendNameSpaceStart();
            AppandClassStart(this.Table.ModelEditRepositoryName, "", string.Format("{0}データリポジトリ", tableAddin.TableDisplayName));

            CreateMethodCreateEditModel();
            CreateMethodFillModelData();
            CreateMethodSetListData();

            foreach (var r in res)
            {
                TableInfo t = Connection.FindTable(r.PrimaryTable.TableName);
                CreateMethodFillModelListData(t);
            }

            CreateMethodCheckInput();
            CreateMethodSetInputData();
            CreateDataUpdate();
            CreateMethodGetSelectItemName();
            CreateDataDelete01();
            AppandClassEnd();
            //AppendNameSpaceEnd();
        }

        private void CreateMethodCreateEditModel()
        {
            FieldInfo keyField = this.Table.GetKeyField();

            CodeBuilder.AppendLine("        public static {0} CreateModel({1} {2}) ", this.Table.ModelEditName, keyField.FieldType.MapName, keyField.FieldNameForParam);
            CodeBuilder.AppendLine("        {");

            CodeBuilder.AppendLine("            {0} model = new {0}();", this.Table.ModelEditName);
            CodeBuilder.AppendLine("            _SetListData(model);");
            CodeBuilder.AppendLine("");
            CodeBuilder.AppendLine("            if ({0} > 0)", keyField.FieldNameForParam);
            CodeBuilder.AppendLine("            {{");
            if (_useCommunicationWrapper)
            {
                CodeBuilder.AppendLine("                var wrapper = new CommunicationWrapper();");
                CodeBuilder.AppendLine("                wrapper.AddInParam(\"{0}\",{1});", keyField.FieldName, keyField.FieldNameForParam);
                CodeBuilder.AppendLine("                wrapper.Execute(\"{0}ItemSelect\");", this.Table.EntityName);
                CodeBuilder.AppendLine("                if (wrapper.OutListParam.Count > 0)");
                CodeBuilder.AppendLine("                {");
                CodeBuilder.AppendLine("                    // データ設定");
                CodeBuilder.AppendLine("                    _FillModelData(model, wrapper.OutListParam[0]);");
                CodeBuilder.AppendLine("                }");
            }
            else
            {
                CodeBuilder.AppendLine("                CommunicationCapsule inParam = new CommunicationCapsule();");
                CodeBuilder.AppendLine("                CommunicationCapsule outParam;");
                CodeBuilder.AppendLine("                CommunicationHelper commHelper = new CommunicationHelper();");
                CodeBuilder.AppendLine("");
                CodeBuilder.AppendLine("                inParam.Param[\"{0}\"] = {0};", keyField.FieldNameForParam);
                CodeBuilder.AppendLine("                commHelper.Execute(CommunicationID.Core, \"{0}ItemSelect\", inParam, out outParam);", this.Table.EntityName);
                CodeBuilder.AppendLine("");
                CodeBuilder.AppendLine("                if (outParam.ListParam.Count > 0)");
                CodeBuilder.AppendLine("                {");
                CodeBuilder.AppendLine("                    // データ設定");
                CodeBuilder.AppendLine("                    _FillModelData(model, outParam.ListParam[0]);");
                CodeBuilder.AppendLine("                }");
            }
            CodeBuilder.AppendLine("            }}");
            CodeBuilder.AppendLine("");
            CodeBuilder.AppendLine("            return model;");
            CodeBuilder.AppendLine("");
            CodeBuilder.AppendLine("        }");

        }

        private void CreateMethodFillModelListData(TableInfo table)
        {
            TableInfoAddin tableAddin = this.Connection.FindTableAddin(table.TableName);
            CodeBuilder.AppendLine("        /// <summary>");
            CodeBuilder.AppendLine("        /// {0}リストデータの取得", tableAddin.TableDisplayName);
            CodeBuilder.AppendLine("        /// </summary>");
            CodeBuilder.AppendLine("        private  static void _FillModelListData_{0}({1} model)", table.EntityName,
                                   this.Table.ModelEditName);
            CodeBuilder.AppendLine("        {");

            if (_useCommunicationWrapper)
            {
                CodeBuilder.AppendLine("                CommunicationWrapper wrapper = new CommunicationWrapper();");
                CodeBuilder.AppendLine("                wrapper.AddInParam(\"{0}\",0);", table.GetKeyField().FieldName);
                CodeBuilder.AppendLine("                wrapper.AddInParam(\"{0}\", string.Empty);", table.GetDisplayValueField().FieldName);
                CodeBuilder.AppendLine("                wrapper.Execute(typeof(PacificSystem.Core.ServerControls.{0}ItemValueText));", table.EntityName);

                CodeBuilder.AppendLine("                List<SelectListItem> item = wrapper.OutListParam.Select(dic => new SelectListItem()");
                CodeBuilder.AppendLine("                {");
                CodeBuilder.AppendLine("                    Text = Converts.ToTryString(dic[\"{0}\"]), Value = Converts.ToTryString(dic[\"{1}\"])", table.GetDisplayValueField().FieldName, table.GetKeyField().FieldName);
                CodeBuilder.AppendLine("                }).ToList();");
            }
            else
            {
                CodeBuilder.AppendLine("            CommunicationCapsule inParam = new CommunicationCapsule();");
                CodeBuilder.AppendLine("            CommunicationCapsule outParam;");
                CodeBuilder.AppendLine("            CommunicationHelper commHelper = new CommunicationHelper();");
                CodeBuilder.AppendLine("");
                CodeBuilder.AppendLine("            inParam.Param[\"{0}\"] = 0;", table.GetKeyField().FieldName);
                CodeBuilder.AppendLine("            inParam.Param[\"{0}\"] = string.Empty; ", table.GetDisplayValueField().FieldName);
                CodeBuilder.AppendLine("            commHelper.Execute(CommunicationID.Core, \"{0}ItemValueText\", inParam, out outParam);", table.EntityName);
                CodeBuilder.AppendLine("");

                CodeBuilder.AppendLine("            List<SelectListItem> item = outParam.ListParam.Select(dic => new SelectListItem");
                CodeBuilder.AppendLine("                {");
                CodeBuilder.AppendLine("                    Text = Converts.ToTryString(dic[\"{0}\"]), ", table.GetDisplayValueField().FieldName);
                CodeBuilder.AppendLine("                    Value = Converts.ToTryString(dic[\"{1}\"])", table.GetKeyField().FieldName);
                CodeBuilder.AppendLine("                }).ToList();");
            }

            //CodeBuilder.AppendLine("            List<SelectListItem> item = new List<SelectListItem>();");
            //CodeBuilder.AppendLine("");
            //CodeBuilder.AppendLine("            foreach (Dictionary<string, object> dic in outParam.ListParam)");
            //CodeBuilder.AppendLine("            {");
            //CodeBuilder.AppendLine("                item.Add(new SelectListItem");
            //CodeBuilder.AppendLine("                {");
            //CodeBuilder.AppendLine("                    Text = Converts.ToTryString(dic[\"{0}\"]),", table.GetDisplayValueField().FieldName);
            //CodeBuilder.AppendLine("                    Value = Converts.ToTryString(dic[\"{0}\"])", table.GetKeyField().FieldName);
            //CodeBuilder.AppendLine("                });");
            //CodeBuilder.AppendLine("            }");
            CodeBuilder.AppendLine("            model.{0} = item;", table.EntityListName);
            CodeBuilder.AppendLine("        }");

        }

        private void CreateMethodFillModelData()
        {
            CodeBuilder.AppendLine("        /// <summary>");
            CodeBuilder.AppendLine("        /// 登録済みデータの設定");
            CodeBuilder.AppendLine("        /// </summary>");
            CodeBuilder.AppendLine("        /// <param name=\"model\"></param>");
            CodeBuilder.AppendLine("        /// <param name=\"item\"></param>");
            CodeBuilder.AppendLine("        private static void _FillModelData({0} model, Dictionary<string, object> item) ", this.Table.ModelEditName);
            CodeBuilder.AppendLine("        {");
            foreach (FieldInfo field in Table.Fields)
            {
                if (field.FieldType.MapName.ToLower().CompareTo("datetime") == 0 && field.GetCSharpTypeNullable())
                {
                    CodeBuilder.AppendLine("            if (item[\"{0}\"] != null && item[\"{0}\"] != DBNull.Value)", field.FieldName);
                    CodeBuilder.AppendLine("                model.{0} = {1}.Value.ToString(\"yyyy-MM-dd\");", field.FieldName, getSingleConvertForSearchModel(field, "item[\"{0}\"]"));
                    CodeBuilder.AppendLine("            else");
                    CodeBuilder.AppendLine("                model.{0} = \"\";", field.FieldName);
                }
                else
                {
                    CodeBuilder.AppendLine("            model.{0} = {1};", field.FieldName, getSingleConvertForSearchModel(field, "item[\"{0}\"]"));
                }
            }
            CodeBuilder.AppendLine("        }");

        }

        private void CreateMethodSetInputData()
        {

            var res = from s in this.Connection.Relations
                      where
                          s.ForeignTable.TableName.ToLower().CompareTo(this.Table.TableName.ToLower()) == 0
                      select s;


            CodeBuilder.AppendLine("        private static Dictionary<string, object> _SetInputData({0} model) ", this.Table.ModelEditName);
            CodeBuilder.AppendLine("        {");
            CodeBuilder.AppendLine("            Dictionary<string, object> result = new Dictionary<string, object>();");
            CodeBuilder.AppendLine("");
            foreach (FieldInfo field in Table.Fields)
            {
                CodeBuilder.AppendLine("            result[\"{0}\"] = {1};", field.FieldName, getSingleConvertForSearchModel(field, "model.{0}"));
            }

            CodeBuilder.AppendLine("");
            CodeBuilder.AppendLine("            //Set Value Field From List<SelectListItem>");
            foreach (var r in res)
            {
                TableInfo t = Connection.FindTable(r.PrimaryTable.TableName);
                if (this.Table.FindField(r.PrimaryTable.KeyFieldName) != null)
                    CodeBuilder.AppendLine("            result[\"{0}\"] = _GetSelectItemName(model.{1}, model.{2});", r.PrimaryTable.ValueFieldName, t.EntityListName, r.PrimaryTable.KeyFieldName);
                else
                    CodeBuilder.AppendLine("            //result[\"{0}\"] = _GetSelectItemName(model.{1}, model.{2});", r.PrimaryTable.ValueFieldName, t.EntityListName, r.PrimaryTable.KeyFieldName);
            }


            CodeBuilder.AppendLine("");
            CodeBuilder.AppendLine("            return result;");
            CodeBuilder.AppendLine("        }");

        }

        private void CreateMethodGetSelectItemName()
        {
            CodeBuilder.AppendLine("        /// <summary>");
            CodeBuilder.AppendLine("        /// 選択item名取得");
            CodeBuilder.AppendLine("        /// </summary>");
            CodeBuilder.AppendLine("        /// <param name=\"list\"></param>");
            CodeBuilder.AppendLine("        /// <param name=\"id\"></param>");
            CodeBuilder.AppendLine("        /// <returns></returns>");
            CodeBuilder.AppendLine("        private static string _GetSelectItemName(IEnumerable<SelectListItem> list, int id) ");
            CodeBuilder.AppendLine("        {");
            CodeBuilder.AppendLine("            string result = string.Empty;");
            CodeBuilder.AppendLine("");
            CodeBuilder.AppendLine("            foreach(SelectListItem item in list)");
            CodeBuilder.AppendLine("            {");
            CodeBuilder.AppendLine("                if (item.Value == Converts.ToTryString(id)) ");
            CodeBuilder.AppendLine("                {");
            CodeBuilder.AppendLine("                    result = item.Text;");
            CodeBuilder.AppendLine("                }");
            CodeBuilder.AppendLine("            }");
            CodeBuilder.AppendLine("");
            CodeBuilder.AppendLine("            return result;");
            CodeBuilder.AppendLine("        }");

        }

        private void CreateMethodSetListData()
        {
            var res = from s in this.Connection.Relations
                      where
                          s.ForeignTable.TableName.ToLower().CompareTo(this.Table.TableName.ToLower()) == 0
                      select s;

            CodeBuilder.AppendLine("        private static void _SetListData( {0} model)", this.Table.ModelEditName);
            CodeBuilder.AppendLine("        {");
            foreach (var r in res)
            {
                TableInfo t = Connection.FindTable(r.PrimaryTable.TableName);
                TableInfoAddin tAddin = this.Connection.FindTableAddin(t.TableName);
                CodeBuilder.AppendLine("            // {0}リストデータの取得", tAddin.TableDisplayName);
                CodeBuilder.AppendLine("            _FillModelListData_{0}(model);", t.EntityName);
            }
            CodeBuilder.AppendLine("        }");

        }

        private void CreateMethodCheckInput()
        {
            CodeBuilder.AppendLine("        /// <summary>");
            CodeBuilder.AppendLine("        /// 登録と承認の場合のチェックする");
            CodeBuilder.AppendLine("        /// </summary>");
            CodeBuilder.AppendLine("        /// <param name=\"request\"></param>");
            CodeBuilder.AppendLine("        /// <param name=\"ms\"></param>");
            CodeBuilder.AppendLine("        public static bool CheckInput({0} request,ModelStateDictionary ms)", this.Table.ModelEditName);
            CodeBuilder.AppendLine("        {");
            CodeBuilder.AppendLine("            return true;");
            CodeBuilder.AppendLine("        }");

        }

        private void CreateDataUpdate()
        {
            CreateDataUpdate01();
            CreateDataUpdate02();
            CreateDataUpdate03();
        }

        private void CreateDataUpdate01()
        {
            CodeBuilder.AppendLine("        /// <summary>");
            CodeBuilder.AppendLine("        /// データ登録");
            CodeBuilder.AppendLine("        /// </summary>");
            CodeBuilder.AppendLine("        /// <param name=\"request\"></param>");
            CodeBuilder.AppendLine("        /// <returns></returns>");
            CodeBuilder.AppendLine("        public static bool DataUpdate({0} request) ", this.Table.ModelEditName);
            CodeBuilder.AppendLine("        {");
            CodeBuilder.AppendLine("            bool isComplete ;");
            CodeBuilder.AppendLine("");
            CodeBuilder.AppendLine("            if (request.{0} > 0) ", this.Table.GetKeyField().FieldName);
            CodeBuilder.AppendLine("            {");
            CodeBuilder.AppendLine("                isComplete = _DataUpdate(request);");
            CodeBuilder.AppendLine("            }");
            CodeBuilder.AppendLine("            else");
            CodeBuilder.AppendLine("            {");
            CodeBuilder.AppendLine("                isComplete = _DataInsert(request);");
            CodeBuilder.AppendLine("            }");
            CodeBuilder.AppendLine("");
            CodeBuilder.AppendLine("            return isComplete;");
            CodeBuilder.AppendLine("        }");
        }

        private void CreateDataUpdate02()
        {
            CodeBuilder.AppendLine("        /// <summary>");
            CodeBuilder.AppendLine("        /// データ追加");
            CodeBuilder.AppendLine("        /// </summary>");
            CodeBuilder.AppendLine("        /// <param name=\"request\"></param>");
            CodeBuilder.AppendLine("        /// <returns></returns>");
            CodeBuilder.AppendLine("        private static bool _DataInsert({0} request)", this.Table.ModelEditName);
            CodeBuilder.AppendLine("        {");
            CodeBuilder.AppendLine("");
            CodeBuilder.AppendLine("            CommunicationCapsule inParam = new CommunicationCapsule();");
            CodeBuilder.AppendLine("            CommunicationCapsule outParam;");
            CodeBuilder.AppendLine("            CommunicationHelper commHelper = new CommunicationHelper();");
            CodeBuilder.AppendLine("");
            CodeBuilder.AppendLine("            inParam.Param = _SetInputData(request);");
            CodeBuilder.AppendLine("");
            CodeBuilder.AppendLine("            bool isComplete =commHelper.Execute(CommunicationID.Core, \"{0}ItemInsert\", inParam, out outParam);", this.Table.EntityName);
            CodeBuilder.AppendLine("");
            CodeBuilder.AppendLine("");
            CodeBuilder.AppendLine("            return isComplete;");
            CodeBuilder.AppendLine("        }");
        }

        private void CreateDataUpdate03()
        {
            CodeBuilder.AppendLine("        /// <summary>");
            CodeBuilder.AppendLine("        /// データ更新");
            CodeBuilder.AppendLine("        /// </summary>");
            CodeBuilder.AppendLine("        /// <param name=\"request\"></param>");
            CodeBuilder.AppendLine("        /// <returns></returns>");
            CodeBuilder.AppendLine("        private static bool _DataUpdate({0} request) ", this.Table.ModelEditName);
            CodeBuilder.AppendLine("        {");
            CodeBuilder.AppendLine("");
            CodeBuilder.AppendLine("            CommunicationCapsule inParam = new CommunicationCapsule();");
            CodeBuilder.AppendLine("            CommunicationCapsule outParam;");
            CodeBuilder.AppendLine("            CommunicationHelper commHelper = new CommunicationHelper();");
            CodeBuilder.AppendLine("");
            CodeBuilder.AppendLine("            inParam.Param = _SetInputData(request);");
            CodeBuilder.AppendLine("");
            CodeBuilder.AppendLine("            bool isComplete =commHelper.Execute(CommunicationID.Core, \"{0}ItemUpdate\", inParam, out outParam);", this.Table.EntityName);
            CodeBuilder.AppendLine("");
            CodeBuilder.AppendLine("            return isComplete;");
            CodeBuilder.AppendLine("        }");

        }


        private void CreateDataDelete01()
        {
            CodeBuilder.AppendLine("");
            CodeBuilder.AppendLine("        public static bool DeleteJudge({0} request)", this.Table.ModelEditName);
            CodeBuilder.AppendLine("        {");
            CodeBuilder.AppendLine("            bool IsComplete = false;");
            CodeBuilder.AppendLine("            CommunicationCapsule inParam = new CommunicationCapsule();");
            CodeBuilder.AppendLine("            CommunicationCapsule outParam = new CommunicationCapsule();");
            CodeBuilder.AppendLine("            CommunicationHelper CommHelper = new CommunicationHelper();");
            CodeBuilder.AppendLine("            inParam.Param = _SetInputData(request);");
            CodeBuilder.AppendLine("            IsComplete = CommHelper.Execute(CommunicationID.Core, \"{0}ItemDeleteCheck\", inParam, out outParam);",this.Table.EntityName);
            CodeBuilder.AppendLine("            if (outParam.ListParam != null && outParam.ListParam.Count > 0 && Converts.ToTryInt(outParam.ListParam[0][\"Num\"]) > 0)");
            CodeBuilder.AppendLine("            {");
            CodeBuilder.AppendLine("                IsComplete = false;");
            CodeBuilder.AppendLine("            }");
            CodeBuilder.AppendLine("            return IsComplete;");
            CodeBuilder.AppendLine("        }");
            CodeBuilder.AppendLine("");
            CodeBuilder.AppendLine("        public static bool Delete({0} request)", this.Table.ModelEditName);
            CodeBuilder.AppendLine("        {");
            CodeBuilder.AppendLine("            bool IsComplete = false;");
            CodeBuilder.AppendLine("            CommunicationCapsule inParam = new CommunicationCapsule();");
            CodeBuilder.AppendLine("            CommunicationCapsule outParam = new CommunicationCapsule();");
            CodeBuilder.AppendLine("            CommunicationHelper CommHelper = new CommunicationHelper();");
            CodeBuilder.AppendLine("            inParam.Param = _SetInputData(request);");
            CodeBuilder.AppendLine("            IsComplete = CommHelper.Execute(CommunicationID.Core, \"{0}ItemDelete\", inParam, out outParam);", this.Table.EntityName);
            CodeBuilder.AppendLine("            return IsComplete;");
            CodeBuilder.AppendLine("        }");
        }

        private string GetSingleDeclare(FieldInfo field)
        {
            return string.Format("public {0} {1} {{get;set;}}", field.FieldType.MapName, field.FieldName);
        }

        private string getSingleDeclareForSearchModel(FieldInfo field)
        {
            string type = field.FieldType.MapName;
            if (_changeDateTimeToString && field.Nullabled && type.ToLower().CompareTo("datetime") == 0)
                type = "string";

            return string.Format("public {0} {1} {{get;set;}}", type, field.FieldName);
        }


        /// <summary>
        /// declare variable and get value from InParam.Param
        /// </summary>
        /// <param name="field"></param>
        /// <returns></returns>
        private string GetSingleConvert(FieldInfo field, string format)
        {
            //Converts.ToTryInt({0},0)
            string method = GeneratorCode.Pasys.Converts.ConvertsMethodName(field);
            string fName = string.Format(format, field.FieldName);
            return string.Format(method, fName);
        }

        private string getSingleConvertForSearchModel(FieldInfo field, string format)
        {
            string method = GeneratorCode.Pasys.Converts.ConvertsMethodName(field, true);
            string fName = string.Format(format, field.FieldName);
            return string.Format(method, fName);
        }

        protected override void AppendUsing()
        {
            CodeBuilder.AppendLine("using System;");
            CodeBuilder.AppendLine("using System.Collections.Generic;");
            CodeBuilder.AppendLine("using System.ComponentModel.DataAnnotations;");
            CodeBuilder.AppendLine("using System.Linq;");
            CodeBuilder.AppendLine("using System.Web;");
            CodeBuilder.AppendLine("using System.Web.Mvc;");
            CodeBuilder.AppendLine("using System.Web.Configuration;");
            CodeBuilder.AppendLine("using PacificSystem.CommunicationLibrary;");
            CodeBuilder.AppendLine("using PacificSystem.ConstantLibrary;");
            //CodeBuilder.AppendLine("using PacificSystem.ConstantLibrary.Sessions;");
            CodeBuilder.AppendLine("using PacificSystem.Utility;");
            CodeBuilder.AppendLine("using MvcApplication.Common;");
        }
        protected override void AppandClassStart(string classAdditional, string baseClassName, string functionDesc)
        {
            CodeBuilder.AppendLine("    /// <summary>");
            CodeBuilder.AppendLine("    ///{0}", functionDesc); // 
            CodeBuilder.AppendLine("    /// </summary>");
            if (baseClassName.Trim().Length > 0)
            {
                CodeBuilder.AppendLine("    public class {0}{1}:{2} ", "", classAdditional, baseClassName);
            }
            else
            {
                CodeBuilder.AppendLine("    public class {0}{1} ", "", classAdditional);
            }
            SaveFileName = string.Format("{0}{1}", "", classAdditional);
            CodeBuilder.AppendLine("    {");

        }

        protected override void AppendNameSpaceStart()
        {
            CodeBuilder.AppendLine("");
            CodeBuilder.AppendLine("namespace MvcApplication.Models");
            CodeBuilder.AppendLine("{");
        }


    }
}
